package net.lab1024.sa.admin.module.upstream.api.painet;

import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.lab1024.sa.admin.module.system.config.SystemConfigService;
import net.lab1024.sa.admin.module.system.config.domain.vo.SystemConfigPainetBoxVO;
import net.lab1024.sa.admin.module.upstream.api.BaseApi;
import net.lab1024.sa.admin.module.upstream.api.painet.request.BoxIncomeQueryFrom;
import net.lab1024.sa.admin.module.upstream.api.painet.request.BoxListQueryFrom;
import net.lab1024.sa.admin.module.upstream.api.painet.response.BoxIncomesResponse;
import net.lab1024.sa.admin.module.upstream.api.painet.response.BoxListResponse;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Component
public class PainetBoxApi extends BaseApi {

    private String account;
    private String ak;
    private String as;
    private String serverUrl;


    private final SystemConfigService systemConfigService;
    private final RestTemplate restTemplate;
    private final ObjectMapper objectMapper;

    public PainetBoxApi(SystemConfigService systemConfigService,
                        RestTemplate restTemplate,
                        ObjectMapper objectMapper) {
        this.systemConfigService = systemConfigService;
        this.restTemplate = restTemplate;
        this.objectMapper = objectMapper;
    }

    @Override
    protected void refresh() {
        ResponseDTO<SystemConfigPainetBoxVO> painetBox = systemConfigService.getPainetBox();
        SystemConfigPainetBoxVO painetBoxCache = painetBox.getData();
        this.account = painetBoxCache.getPainetBoxAccount();
        this.ak = painetBoxCache.getPainetBoxAK();
        this.as = painetBoxCache.getPainetBoxAS();
        this.serverUrl = painetBoxCache.getPainetBoxServerUrl();
    }

    private void setHeaders(HttpHeaders headers) {
        headers.set("ak", this.ak);
        String timestamp = DateUtil.currentSeconds() + "";
        headers.set("timestamp", timestamp);
        headers.set("sign", DigestUtils.md5Hex(this.as + "#" + timestamp + "#" + this.account));
    }

    public List<BoxListResponse.Box> getBoxList(BoxListQueryFrom boxListQueryFrom) throws Exception {
        // 将请求体对象转换为JSON字符串
        String jsonRequestBody = objectMapper.writeValueAsString(boxListQueryFrom);

        // 创建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        this.setHeaders(headers);

        // 创建HttpEntity对象
        HttpEntity<String> request = new HttpEntity<>(jsonRequestBody, headers);

        // 发送POST请求
        String url = this.serverUrl + "/boxsupplier/v1/box/list";
        String response = restTemplate.postForObject(url, request, String.class);
        return objectMapper.readValue(response, BoxListResponse.class).getBoxList();
    }

    public List<BoxIncomesResponse.BoxBandwidth> incomes(BoxIncomeQueryFrom queryFrom) throws Exception {
        // 将请求体对象转换为JSON字符串
        String jsonRequestBody = objectMapper.writeValueAsString(queryFrom);

        // 创建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        this.setHeaders(headers);

        // 创建HttpEntity对象
        HttpEntity<String> request = new HttpEntity<>(jsonRequestBody, headers);

        // 发送POST请求
        String url = this.serverUrl + "/boxsupplier/v1/supplier/income";
        String response = restTemplate.postForObject(url, request, String.class);
        return objectMapper.readValue(response, BoxIncomesResponse.class)
                .getIncomes()
                .stream()
                .findFirst()
                .orElseGet(BoxIncomesResponse.Income::new)
                .getBoxBandwidths();
    }
}
